import json

from django.shortcuts import render
from django.http import JsonResponse
from django.views import View
import jwt
import time
from .models import Auth_admin, Roles, Permissions, Role_permissions, User_role


# 登录
class Login(View):
    def get(self, request):
        pass

    def post(self, request):
        data = json.loads(request.body)
        print(data)
        auth = Auth_admin.objects.filter(username=data['username'], password=data['password']).values()
        if len(auth):
            d = {
                # 公共声明
                'exp': time.time() + 18000,  # (Expiration Time) 此token的过期时间的时间戳
                'iat': time.time(),  # (Issued At) 指明此创建时间的时间戳
                'iss': 'ms',  # 指明此token的签发者
                # 私有声明
                'data': {
                    'username': data['username'],
                    'password': data['password'],
                    'timestamp': time.time()
                }
            }
            token = jwt.encode(d, '123456', algorithm='HS256')
            jwt_decode = jwt.decode(token, '123456', issuer='ms', algorithms=['HS256'])
            print(jwt_decode)
            return JsonResponse({'msg': '登录成功', 'status': 200, 'token': token})
        else:
            return JsonResponse({'msg': '登录失败', 'status': 400})


# 用户接口
class Admin(View):
    def get(self, request):
        data = Auth_admin.objects.all().values()
        return JsonResponse({'msg': '获取所有用户信息成功', 'status': 200, 'data': list(data)})

    def post(self, request):
        data = json.loads(request.body)
        print(data)
        Auth_admin.objects.create(
            username=data['username'],
            password=data['password'],
            head=data['head'],
            token=1111,
            out_time=time.time()
        )
        return JsonResponse({'msg': '创建成功', 'status': 201})

    def put(self, request):
        pass

    def delete(self, request):
        pass


# 角色数据接口
class Role(View):
    def get(self, request):
        user = request.GET.get('username')
        if user is None:
            data = Roles.objects.all().values()
            return JsonResponse({'msg': '获取所有角色信息成功', 'status': 200, 'data': list(data)})
        else:
            user_data = Auth_admin.objects.filter(username='admin').values()  # 通过用户名获取用户信息
            role_data = User_role.objects.filter(user_id_id=user_data[0]['id']).values()  # 查询用户角色表通过用户id查到角色
            role = Roles.objects.filter(id=role_data[0]['role_id_id']).values()
            print(role)
            return JsonResponse({'msg': '获取所有角色信息成功', 'status': 200, 'role': role[0]})

    def post(self, request):
        pass

    def put(self, request):
        pass

    def delete(self, request):
        pass


# 权限数据接口
class Permission(View):
    def get(self, request):
        role = request.GET.get('role_name')
        if role is None:
            data = Permissions.objects.all().values()
            return JsonResponse({'msg': '获取所有权限信息成功', 'status': 200, 'data': list(data)})
        else:
            rdata = Roles.objects.filter(role_name=role).values()
            role_data = Role_permissions.objects.filter(role_id=rdata[0]['id']).values()
            role_list = []
            for role in list(role_data):
                per = Permissions.objects.filter(id=role['per_id_id']).values()
                role_list.append({'role_name': per[0]['per_name'], 'path': per[0]['path']})
            print(role_list)
            return JsonResponse({'msg': '获取所有权限信息成功', 'status': 200, 'data': list(role_list)})

    def post(self, request):
        pass

    def put(self, request):
        pass

    def delete(self, request):
        pass
